package com.igeek.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.oa.entity.Employee;
import com.igeek.oa.entity.Officesupplies;
import com.igeek.oa.mapper.EmployeeMapper;
import com.igeek.oa.result.Result;
import com.igeek.oa.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-28
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public static final String USER_SIGN_KEY="sign::";
    public static final String USER_SIGN="today::";
    @Override
    public List<Employee> getEmployees() {
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Employee> employees = employeeMapper.selectList(employeeLambdaQueryWrapper);
        return employees;
    }

    @Override
    public Result SignByRedis(String eId) {
        LocalDateTime localDateTime = LocalDateTime.now();
        int month = localDateTime.getMonthValue() + 1;
        if(StringUtils.hasLength(eId)){
            if(stringRedisTemplate.opsForValue().get(USER_SIGN+eId+"_"+month) != null){
                return new Result(false,"签到失败","请勿重复签到");
            }
            else {
                if(stringRedisTemplate.opsForValue().get(USER_SIGN_KEY+eId+"_"+month) != null){
                    //存在该员工
                    stringRedisTemplate.opsForValue().increment(USER_SIGN_KEY+eId+"_"+month);
                    stringRedisTemplate.opsForValue().set(USER_SIGN+eId+"_"+month,"今日打卡成功", 5, TimeUnit.HOURS);
                    System.out.println("打卡次数"+stringRedisTemplate.opsForValue().get(USER_SIGN_KEY+eId+"_"+month));
                    String count = stringRedisTemplate.opsForValue().get(USER_SIGN_KEY+eId+"_"+month);
                    return new Result(true,"打卡成功",count);

                }
                else{
                    //第一次
                    stringRedisTemplate.opsForValue().set(USER_SIGN_KEY+eId+"_"+month,"1");
                    stringRedisTemplate.opsForValue().set(USER_SIGN+eId+"_"+month,"今日打卡成功",5,TimeUnit.HOURS);
                    System.out.println("第一次打卡："+stringRedisTemplate.opsForValue().get(USER_SIGN+eId+"_"+month));
                    String count = stringRedisTemplate.opsForValue().get(USER_SIGN_KEY+eId+"_"+month);
                    return new Result(true,"打卡成功",count);
                }
            }
        }
        return new Result(false,"签到失败","未获取员工信息");
    }
}
